A relationship that often arises between components of a software system is the producer/consumer pattern. In such a pattern, a group of one or more producers need to communicate messages or other data to a group of one or more consumers. For convenience, the consumed items are referred to herein generally as “messages,” but may include objects, data structures, strings, and/or any other data that a producer can communicate to a consumer.
Communication between producers and consumers is sometimes implemented using a queue data structure. The queue effectively decouples the production and consumption activities by enabling a producer to insert a message into the queue and a consumer to independently retrieve that message later.
In concurrent systems, where different threads concurrently perform the production and consumption activities, a thread-safe, concurrent queue may be used. A concurrent queue allows thread-safe access by using producer and consumer locks to coordinate access by producers and by consumers.
The capability of a concurrent queue to decouple production and consumption activity offers various design advantages. For example, because the concurrent queue enables asynchronous communication between the producer and consumer, a producing thread may leave a message in the queue and perform other tasks rather than wait on a consuming thread to retrieve the message. Thus, activity between the two threads need not be tightly coordinated. Moreover, additional consumption or production threads may be added or removed without the need to modify the other threads.